home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / ServiceProxy.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  11KB  |  337 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import weakref
  5. import re
  6. import os
  7. import sys
  8. from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError, NoOptionError
  9. from urlparse import urlparse
  10. from ZSI import TC
  11. from ZSI.client import _Binding
  12. from ZSI.generate import commands, containers
  13. from ZSI.schema import GED, GTD
  14. import wstools
  15.  
  16. def _urn_to_module(urn):
  17.     return '%s_types' % re.sub(_urn_to_module.regex, '_', urn)
  18.  
  19. _urn_to_module.regex = re.compile('[\\W]')
  20.  
  21. class ServiceProxy:
  22.     
  23.     def __init__(self, wsdl, url = None, service = None, port = None, cachedir = os.path.join(os.path.expanduser('~'), '.zsi_service_proxy_dir'), asdict = True, lazy = False, pyclass = False, force = False, **kw):
  24.         self._asdict = asdict
  25.         self._url = url
  26.         self._kw = kw
  27.         self._wsdl = wstools.WSDLTools.WSDLReader().loadFromURL(wsdl)
  28.         if not service:
  29.             pass
  30.         self._service = self._wsdl.services[0]
  31.         self.__doc__ = self._service.documentation
  32.         if not port:
  33.             pass
  34.         self._port = self._service.ports[0]
  35.         self._name = self._service.name
  36.         self._methods = { }
  37.         self._cachedir = cachedir
  38.         self._lazy = lazy
  39.         self._pyclass = pyclass
  40.         self._force = force
  41.         port = self._port
  42.         binding = port.getBinding()
  43.         portType = binding.getPortType()
  44.         for port in self._service.ports:
  45.             for item in port.getPortType().operations:
  46.                 
  47.                 try:
  48.                     callinfo = wstools.WSDLTools.callInfoFromWSDL(port, item.name)
  49.                 except:
  50.                     continue
  51.  
  52.                 method = MethodProxy(self, callinfo)
  53.                 setattr(self, item.name, method)
  54.                 self._methods.setdefault(item.name, []).append(method)
  55.             
  56.         
  57.         self._mod = self._load(wsdl)
  58.  
  59.     
  60.     def _load(self, location):
  61.         cachedir = self._cachedir
  62.         if not os.path.isdir(cachedir):
  63.             os.mkdir(cachedir)
  64.         
  65.         file = os.path.join(cachedir, '.cache')
  66.         section = 'TYPES'
  67.         cp = ConfigParser()
  68.         
  69.         try:
  70.             cp.readfp(open(file, 'r'))
  71.         except IOError:
  72.             del cp
  73.             cp = None
  74.  
  75.         option = location.replace(':', '-')
  76.         if not (self._force) and cp is not None and cp.has_section(section) and cp.has_option(section, option):
  77.             types = cp.get(section, option)
  78.         elif not self._pyclass:
  79.             
  80.             containers.ContainerBase.func_aname = lambda instnc, n: str(n)
  81.         
  82.         args = [
  83.             '-o',
  84.             cachedir,
  85.             location]
  86.         if self._lazy:
  87.             args.insert(0, '-l')
  88.         
  89.         if self._pyclass:
  90.             args.insert(0, '-b')
  91.         
  92.         files = commands.wsdl2py(args)
  93.         if cp is None:
  94.             cp = ConfigParser()
  95.         
  96.         if not cp.has_section(section):
  97.             cp.add_section(section)
  98.         
  99.         types = filter((lambda f: f.endswith('_types.py')), files)[0]
  100.         cp.set(section, option, types)
  101.         cp.write(open(file, 'w'))
  102.         if os.path.abspath(cachedir) not in sys.path:
  103.             sys.path.append(os.path.abspath(cachedir))
  104.         
  105.         mod = os.path.split(types)[-1].rstrip('.py')
  106.         return __import__(mod)
  107.  
  108.     
  109.     def _load_schema(self, location, xml = None):
  110.         cachedir = self._cachedir
  111.         if not os.path.isdir(cachedir):
  112.             os.mkdir(cachedir)
  113.         
  114.         file = os.path.join(cachedir, '.cache')
  115.         section = 'TYPES'
  116.         cp = ConfigParser()
  117.         
  118.         try:
  119.             cp.readfp(open(file, 'r'))
  120.         except IOError:
  121.             del cp
  122.             cp = None
  123.  
  124.         option = location.replace(':', '-')
  125.         if cp is not None and cp.has_section(section) and cp.has_option(section, option):
  126.             types = cp.get(section, option)
  127.         elif not self._pyclass:
  128.             
  129.             containers.ContainerBase.func_aname = lambda instnc, n: str(n)
  130.         
  131.         XMLSchema = XMLSchema
  132.         import ZSI.wstools
  133.         reader = XMLSchema.SchemaReader(base_url = location)
  134.         if xml is not None and isinstance(xml, basestring):
  135.             schema = reader.loadFromString(xml)
  136.         elif xml is not None:
  137.             raise RuntimeError, 'Unsupported: XML must be string'
  138.         elif not os.path.isfile(location):
  139.             schema = reader.loadFromURL(location)
  140.         else:
  141.             schema = reader.reader.loadFromFile(location)
  142.         
  143.         class options('options', ()):
  144.             output_dir = cachedir
  145.             schema = True
  146.             simple_naming = False
  147.             address = False
  148.             lazy = self._lazy
  149.             complexType = self._pyclass
  150.  
  151.         schema.location = location
  152.         files = commands._wsdl2py(options, schema)
  153.         if cp is None:
  154.             cp = ConfigParser()
  155.         
  156.         if not cp.has_section(section):
  157.             cp.add_section(section)
  158.         
  159.         types = filter((lambda f: f.endswith('_types.py')), files)[0]
  160.         cp.set(section, option, types)
  161.         cp.write(open(file, 'w'))
  162.         if os.path.abspath(cachedir) not in sys.path:
  163.             sys.path.append(os.path.abspath(cachedir))
  164.         
  165.         mod = os.path.split(types)[-1].rstrip('.py')
  166.         return __import__(mod)
  167.  
  168.     
  169.     def _call(self, name, soapheaders):
  170.         
  171.         def call_closure(*args, **kwargs):
  172.             if len(args) and len(kwargs):
  173.                 raise TypeError, 'Use positional or keyword argument only.'
  174.             
  175.             if len(args) > 0:
  176.                 raise TypeError, 'Not supporting SOAPENC:Arrays or XSD:List'
  177.             
  178.             if len(kwargs):
  179.                 args = kwargs
  180.             
  181.             callinfo = getattr(self, name).callinfo
  182.             for method in self._methods[name]:
  183.                 if len(method.callinfo.inparams) == len(kwargs):
  184.                     callinfo = method.callinfo
  185.                     continue
  186.             
  187.             if not self._url:
  188.                 pass
  189.             binding = _Binding(url = callinfo.location, soapaction = callinfo.soapAction, **self._kw)
  190.             kw = dict(unique = True)
  191.             if callinfo.use == 'encoded':
  192.                 kw['unique'] = False
  193.             
  194.             if callinfo.style == 'rpc':
  195.                 request = TC.Struct(None, ofwhat = [], pname = (callinfo.namespace, name), **kw)
  196.                 response = TC.Struct(None, ofwhat = [], pname = (callinfo.namespace, name + 'Response'), **kw)
  197.                 if len(callinfo.getInParameters()) != len(args):
  198.                     raise RuntimeError('expecting "%s" parts, got %s' % str(callinfo.getInParameters(), str(args)))
  199.                 
  200.                 for msg, pms in ((request, callinfo.getInParameters()), (response, callinfo.getOutParameters())):
  201.                     msg.ofwhat = []
  202.                     for klass in pms:
  203.                         part = None
  204.                         if klass is None:
  205.                             if part.type:
  206.                                 klass = (filter,)((lambda gt: part.type == gt.type), TC.TYPES)
  207.                                 if len(klass) == 0:
  208.                                     klass = (filter,)((lambda gt: part.type[1] == gt.type[1]), TC.TYPES)
  209.                                     if not len(klass):
  210.                                         klass = [
  211.                                             TC.Any]
  212.                                     
  213.                                 
  214.                                 if len(klass) > 1:
  215.                                     klass = filter((lambda i: i.__dict__.has_key('type')), klass)
  216.                                 
  217.                                 klass = klass[0]
  218.                             else:
  219.                                 klass = TC.Any
  220.                         
  221.                         msg.ofwhat.append(klass(part.name))
  222.                     
  223.                     msg.ofwhat = tuple(msg.ofwhat)
  224.                 
  225.                 if not args:
  226.                     args = { }
  227.                 
  228.             else:
  229.                 ipart = callinfo.getInParameters()
  230.                 opart = callinfo.getOutParameters()
  231.                 if len(ipart) != 1 and not (ipart[0].element_type) or ipart[0].type is None:
  232.                     raise RuntimeError, 'Bad Input Message "%s"' % callinfo.name
  233.                 
  234.                 if len(opart) not in (0, 1) and not (opart[0].element_type) or opart[0].type is None:
  235.                     raise RuntimeError, 'Bad Output Message "%s"' % callinfo.name
  236.                 
  237.                 ipart = ipart[0]
  238.                 request = GED(*ipart.type)
  239.                 response = None
  240.                 if opart:
  241.                     response = GED(*opart[0].type)
  242.                 
  243.             msg = args
  244.             if self._asdict:
  245.                 if not msg:
  246.                     msg = dict()
  247.                 
  248.                 self._nullpyclass(request)
  249.             elif request.pyclass is not None:
  250.                 if type(args) is dict:
  251.                     msg = request.pyclass()
  252.                     msg.__dict__.update(args)
  253.                 elif type(args) is list and len(args) == 1:
  254.                     msg = request.pyclass(args[0])
  255.                 else:
  256.                     msg = request.pyclass()
  257.             
  258.             binding.Send(None, None, msg, requesttypecode = request, soapheaders = soapheaders, encodingStyle = callinfo.encodingStyle)
  259.             if response is None:
  260.                 return None
  261.             
  262.             if self._asdict:
  263.                 self._nullpyclass(response)
  264.             
  265.             return binding.Receive(replytype = response, encodingStyle = callinfo.encodingStyle)
  266.  
  267.         return call_closure
  268.  
  269.     
  270.     def _nullpyclass(cls, typecode):
  271.         typecode.pyclass = None
  272.         if not hasattr(typecode, 'ofwhat'):
  273.             return None
  274.         
  275.         if type(typecode.ofwhat) not in (list, tuple):
  276.             cls._nullpyclass(typecode.ofwhat)
  277.         else:
  278.             for i in typecode.ofwhat:
  279.                 cls._nullpyclass(i)
  280.             
  281.  
  282.     _nullpyclass = classmethod(_nullpyclass)
  283.  
  284.  
  285. class MethodProxy:
  286.     
  287.     def __init__(self, parent, callinfo):
  288.         self.__name__ = callinfo.methodName
  289.         self.__doc__ = callinfo.documentation
  290.         self.callinfo = callinfo
  291.         self.parent = weakref.ref(parent)
  292.         self.soapheaders = []
  293.  
  294.     
  295.     def __call__(self, *args, **kwargs):
  296.         return self.parent()._call(self.__name__, self.soapheaders)(*args, **kwargs)
  297.  
  298.     
  299.     def add_headers(self, **headers):
  300.         
  301.         class _holder:
  302.             pass
  303.  
  304.         
  305.         def _remap(pyobj, **d):
  306.             pyobj.__dict__ = d
  307.             for k, v in pyobj.__dict__.items():
  308.                 if type(v) is not dict:
  309.                     continue
  310.                 
  311.                 pyobj.__dict__[k] = p = _holder()
  312.                 _remap(p, **v)
  313.             
  314.  
  315.         for k, v in headers.items():
  316.             h = (filter,)((lambda i: k in i.type), self.callinfo.inheaders)[0]
  317.             if h.element_type != 1:
  318.                 raise RuntimeError, 'not implemented'
  319.             
  320.             typecode = GED(*h.type)
  321.             if typecode is None:
  322.                 raise RuntimeError, 'no matching element for %s' % str(h.type)
  323.             
  324.             pyclass = typecode.pyclass
  325.             if pyclass is None:
  326.                 raise RuntimeError, 'no pyclass for typecode %s' % str(h.type)
  327.             
  328.             if type(v) is not dict:
  329.                 pyobj = pyclass(v)
  330.             else:
  331.                 pyobj = pyclass()
  332.                 _remap(pyobj, **v)
  333.             self.soapheaders.append(pyobj)
  334.         
  335.  
  336.  
  337.